package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SignUpPageContent implements PageContent {

	private VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel signUpContent = new VerticalPanel();
	private HorizontalPanel signUpWithImage = new HorizontalPanel();
	private VerticalPanel signUpPanel = new VerticalPanel();
	private Image umlImage = new Image();
	private Label signUpLabel = new Label();
	private Label firstNameLabel = new Label();
	private Label lastNameLabel = new Label();
	private Label emailLabel = new Label(); 
	private Label re_emailLabel = new Label();
	private Label passwordLabel = new Label() ;
	private Label re_passwordLabel = new Label();
	private TextBox firstName = new TextBox();
	private TextBox lastName = new TextBox();
	private TextBox email = new TextBox();
	private TextBox username = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private PasswordTextBox re_password = new PasswordTextBox();
	private Label checkFirstNameLabel = new Label();
	private Image signUpButton = new Image("images/signUp.png");
	private CheckBox acceptPrivacy = new CheckBox();
	private Image header = new Image();
	private Image footer = new Image();
	private DialogBox errorDialogBox = new DialogBox();
	
	@Override
	public void setPageContent() {
		
		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");
		
		signUpLabel.setText("Sign Up");
		signUpLabel.setStyleName("pagesHeader");
		
		firstNameLabel.setText("First Name :");
		firstNameLabel.setStyleName("labels");
		
		checkFirstNameLabel.setText("You Must Insert Your First Name");
		checkFirstNameLabel.setStyleName("textBoxCheck");
		checkFirstNameLabel.setVisible(false);
		
		lastNameLabel.setText("Last Name :");
		lastNameLabel.setStyleName("labels");
		
		emailLabel.setText("E-mail :");
		emailLabel.setStyleName("labels");
		
		re_emailLabel.setText("Username :");
		re_emailLabel.setStyleName("labels");
		
		passwordLabel.setText("Password :");
		passwordLabel.setStyleName("labels");
		
		re_passwordLabel.setText("Confirm Password :");
		re_passwordLabel.setStyleName("labels");
		
		firstName.setStyleName("textBox");
		lastName.setStyleName("textBox");
		email.setStyleName("textBox");
		username.setStyleName("textBox");
		password.setStyleName("textBox");
		re_password.setStyleName("textBox");
		
		signUpButton.setStyleName("ProDesUMLButton");
		acceptPrivacy.setText("Yes, I have read and accepted the ProDesUML Terms of Use and " +
				"the Privacy Statement.");
		acceptPrivacy.setStyleName("labels");
		
		footer.setUrl("images/footer.png");
		footer.setWidth("1201px");
		
		signUpContent.add(new HeaderButtons().getHeaderButtons());
		signUpContent.add(signUpLabel);
		signUpContent.add(checkFirstNameLabel);
		signUpContent.add(firstNameLabel);
		signUpContent.add(firstName);
		signUpContent.add(lastNameLabel);
		signUpContent.add(lastName);
		signUpContent.add(emailLabel);
		signUpContent.add(email);
		signUpContent.add(re_emailLabel);
		signUpContent.add(username);
		signUpContent.add(passwordLabel);
		signUpContent.add(password);
		signUpContent.add(re_passwordLabel);
		signUpContent.add(re_password);
//		signUpContent.add(new HTML("<br>"));
		signUpContent.add(acceptPrivacy);
		signUpContent.add(signUpButton);

		
		
		signUpButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				validateSignUp();
			}
		});
		
		signUpButton.addMouseOverHandler(new MouseOverHandler() {
			@Override
			public void onMouseOver(MouseOverEvent event) {
				signUpButton.setUrl("images/signUp_.png");
			}
		});
		
		signUpButton.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				signUpButton.setUrl("images/signUp.png");
			}
		});

		umlImage.setUrl("images/image.jpg");
		umlImage.setStyleName("homeLeftImage");
		
		signUpWithImage.add(umlImage);
		signUpWithImage.add(signUpContent);
		
		signUpPanel.add(signUpWithImage);
		signUpPanel.add(new PrivacyBar().getprivacyBar());
		signUpPanel.setStyleName("border");
		
		mainPanel.add(header);
		mainPanel.add(signUpPanel);
		mainPanel.add(footer);
	}

	@Override
	public VerticalPanel getPageContent() {
		setPageContent();
		return mainPanel;
	}

	@SuppressWarnings("deprecation")
	public void validateSignUp(){
		String errorMsg = "";
		if (firstName.getText().equals("")) {
			errorMsg += "-	You Must Enter Your First Name.<br>";
		}
		if (lastName.getText().equals("")) {
			errorMsg += "-	You Must Enter Your Last Name.<br>";
		}
		if (email.getText().equals("")) {
			errorMsg += "-	You Must Enter Your E-Mail.<br>";
		}
		if (email.getText().endsWith(".")) {
			errorMsg += "-	Invalid E-mail.<br>";
		}
		if (username.getText().equals("")) {
			errorMsg += "-	You Must Enter Your Username.<br>";
		}
		if (password.getText().equals("")) {
			errorMsg += "-	You Must Enter Your Password.<br>";
		}
		if (re_password.getText().equals("")) {
			errorMsg += "-	You Must Re-Enter Your Password.<br>";
		}
		if (password.getText().length() > 0 && re_password.getText().length() > 0 &&
				!(password.getText().equals(re_password.getText()))) {
			errorMsg += "-	Your password doesn't match.<br>";
		}
		if (acceptPrivacy.isChecked() == false) {
			errorMsg += "-	You Must Accept Our Term Privacy.<br>";
		}
		
		if (errorMsg.equals("")) {
			History.newItem("Account");
		}
		else {
			errorMsg += "---------------------------------------------<br>";
			VerticalPanel errorDialogBoxVP = new VerticalPanel();
			HTML msg = new HTML(errorMsg);
			msg.setStyleName("errorDialogBoxText");
			Button closeDialogBox = new Button("Close");
			closeDialogBox.setStyleName("errorDialogBoxButton");
			closeDialogBox.setFocus(true);
			errorDialogBoxVP.add(msg);
			errorDialogBoxVP.add(closeDialogBox);
			errorDialogBox.setText("Error message");
			errorDialogBox.setWidget(errorDialogBoxVP);
			errorDialogBox.setAnimationEnabled(true);
			errorDialogBox.center();
			
			closeDialogBox.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					errorDialogBox.hide();
				}
			});
		}
		
	}
	
}
